package com.lonzie.zebrascandemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.TextView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.lonzie.zebrascandemo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    final String actionName = "dev.demo.action"; // 配置: Intent输出:Intent 操作
    final String categoryName = "dev.demo.category"; // 配置: Intent输出:Intent 类别
    final String zberaKey = "com.symbol.datawedge.data_string";// 斑马固定值

    TextView txtDemo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtDemo = findViewById(R.id.txtDemo);

        // 注册
        IntentFilter filter = new IntentFilter();
        filter.addAction(actionName);
        filter.addCategory(categoryName);
        this.registerReceiver(broadcastReceiver, filter);
    }

    /**
     * 扫描广播回调
     */
    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(actionName)) {
                String barcode = intent.getStringExtra(zberaKey);
                txtDemo.post(() -> {
                    txtDemo.setText(barcode);
                });
            }
        }
    };

    @Override
    protected void onDestroy() {
        //销毁
        this.unregisterReceiver(broadcastReceiver);
        super.onDestroy();
    }
}